package pers.bbw.crr.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import pers.bbw.crr.dao.EquipmentMapper;
import pers.bbw.crr.dto.EquipmentSearchDto;
import pers.bbw.crr.entity.Equipment;
import pers.bbw.crr.service.EquipmentService;
import pers.bbw.crr.util.FileUtil;

import javax.annotation.Resource;
import java.util.Date;

/**
 * equipmentService业务层处理
 *
 * @author bbw
 * @date 2024-12-13
 */
@Service
public class EquipmentServiceImpl implements EquipmentService {
    @Resource
    private EquipmentMapper equipmentMapper;

    /**
     * 查询equipment
     *
     * @param id equipment主键
     * @return equipment
     */
    @Override
    public Equipment selectEquipmentById(int id) {
        return equipmentMapper.selectEquipmentById(id);
    }

    /**
     * 查询equipment列表
     *
     * @param dto equipment
     * @return equipment
     */
    @Override
    public PageInfo<Equipment> selectEquipmentList(EquipmentSearchDto dto) {
        Equipment equipment = new Equipment();
        BeanUtil.copyProperties(dto, equipment);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        PageInfo<Equipment> pageInfo = new PageInfo<>(equipmentMapper.selectEquipmentList(equipment));
        return pageInfo;
    }

    /**
     * 新增equipment
     *
     * @param equipment equipment
     * @return 结果
     */
    @Override
    public void insertEquipment(Equipment equipment, MultipartFile file) {
        saveAndUpload(equipment, file);
    }

    /**
     * 修改equipment
     *
     * @param equipment equipment
     * @return 结果
     */
    @Override
    public void updateEquipment(Equipment equipment, MultipartFile file) {
        saveAndUpload(equipment, file);
    }

    /**
     * 批量删除equipment
     *
     * @param ids 需要删除的equipment主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentByIds(int[] ids) {
        return equipmentMapper.deleteEquipmentByIds(ids);
    }

    /**
     * 删除equipment信息
     *
     * @param id equipment主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentById(int id) {
        return equipmentMapper.deleteEquipmentById(id);
    }

    private void saveAndUpload(Equipment equipment, MultipartFile file) {
        if (equipment.getId() == null) {//新增
            if (file != null) {
                String fileUrl = FileUtil.upload(file);
                equipment.setFileUrl(fileUrl);
                equipment.setCreateTime(new Date());
            }
            equipmentMapper.insertEquipment(equipment);
        } else {//修改
            String fileUrl = equipmentMapper.selectFileUrlById(equipment.getId());
            if (StrUtil.isNotEmpty(fileUrl)) {//原图片不为空
                FileUtil.delete(fileUrl);
            }
            if (file != null) { //上传图片有改动
                String newFile = FileUtil.upload(file);
                equipment.setFileUrl(newFile);
            }
            equipmentMapper.updateEquipment(equipment);
        }
    }
}

